package com.sa07.db;

import com.sa07.db.impl.H2Session;
import com.sa07.db.impl.OracleSession;

/**
 * 默认Session工厂实现
 * 
 * @author Long.shangbo
 * 
 *         2014-2-15 下午7:01:27
 */
public abstract class SessionFactory {

	protected DbConfig dbConfig = null;
	
	private ThreadLocal<Session> sessionLocal = new ThreadLocal<Session>();
	
	/**
	 * 获得数据库配置
	 * @return
	 */
	public DbConfig getDbConfig(){
		return dbConfig;
	}
	
	/**
	 * 构建Session工厂
	 */
	public abstract void buildFactory(DbConfig config);
	
	/**
	 * 创建Session对象
	 * 
	 * @return
	 */
	public Session createSession() {
		Session session = sessionLocal.get();
		
		if(session != null) return session;
		String dbType = dbConfig.getDbType();

		if (DbType.ORACLE.getDbTypeName().equalsIgnoreCase(dbType)) {
			session = new OracleSession(dbConfig);
		} else if (DbType.SQLITE.getDbTypeName().equalsIgnoreCase(dbType)) {
			
		} else if (DbType.DB2.getDbTypeName().equalsIgnoreCase(dbType)) {

		} else if (DbType.DERBY.getDbTypeName().equalsIgnoreCase(dbType)) {

		} else if (DbType.H2.getDbTypeName().equalsIgnoreCase(dbType)) {
			session = new H2Session(dbConfig);
		} else if (DbType.MYSQL.getDbTypeName().equalsIgnoreCase(dbType)) {

		} else if (DbType.SQLSERVER.getDbTypeName().equalsIgnoreCase(dbType)) {

		} else if (DbType.SYBASE_ASA.getDbTypeName().equalsIgnoreCase(dbType)) {

		} else if (DbType.SYBASE_IQ.getDbTypeName().equalsIgnoreCase(dbType)) {

		} else {
			session = new H2Session(dbConfig);
		}

		sessionLocal.set(session);
		return session;
	}
}
